package org.eenie.cartoonist.widget;

import android.content.Context;
import android.graphics.Point;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Scroller;

/**
 * Created by Eenie on 2016/8/23 9:36.
 * Des：
 */

public class TBMerchandiseLayout extends ViewGroup {
    private Context mContext;
    private View mTopView; //顶部的布局
    private View mBottomView;//底部的布局
    private int screenWidth;//屏幕宽度
    private int screenHeight;//屏幕高度
    private int layoutWidth;
    private int layoutHeight;
    private Scroller mScroller;
    private Point lastPoint = new Point();
    private final int statue_close = 0;
    private final int statue_open = 1;
    private final int statue_scroll = 2;

    private double damp = 2.8; //阻尼系数
    private double judge = 0.25;//用来判断滑动多少触发打开/关闭
    int actionFlag = 0;
    private int statue = 0;

    private boolean interrupt;//是否拦截滑动事件


    public TBMerchandiseLayout(Context context) {
        this(context, null);
    }

    public TBMerchandiseLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        this.mContext = context;
        mScroller = new Scroller(context);
    }


    @Override
    protected void onLayout(boolean need, int l, int t, int r, int b) {

        //layout child view if need
        if (need) {
            int topViewWidth = mTopView.getMeasuredWidth();
            int topViewHeight = mTopView.getMeasuredHeight();

            int bottomWidth = mBottomView.getMeasuredWidth();
            int bottomHeight = mBottomView.getMeasuredHeight();

            mTopView.layout(0, 0, topViewWidth, topViewHeight);
            mBottomView.layout(0, topViewHeight, bottomWidth, topViewHeight + bottomHeight);
        }


    }


    // TODO: 2016/8/23 拦截一切动作、暂时测试
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
//
//
//
//        if (statue == statue_close) {
//            switch (ev.getAction()) {
//                case MotionEvent.ACTION_DOWN:
//                    lastPoint.set((int) getX(), (int) getY());
//                    break;
//                case MotionEvent.ACTION_MOVE:
//                    Point currPoint = new Point((int) getX(), (int) getY());
//                    int dy = currPoint.y - lastPoint.y;
//
//                    System.out.println("here");
//                    if (mTopView.getScrollY() == 896) {
//                        //上滑
//                        return true;
//                    }
//                    lastPoint.set(currPoint.x, currPoint.y);
//                    break;
//                case MotionEvent.ACTION_UP:
//
//                    break;
//            }
//        }
//

        return super.onInterceptTouchEvent(ev);
    }


    public void setInterrupt(boolean interrupt) {
        this.interrupt = interrupt;
    }


    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        return super.dispatchTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                lastPoint.x = (int) event.getX();
                lastPoint.y = (int) event.getY();
                actionFlag = 0;
                break;
            case MotionEvent.ACTION_MOVE:
                Point currPoint = new Point((int) event.getX(), (int) event.getY());
                int dy = currPoint.y - lastPoint.y;
                if (dy > 0) {
                    //下滑
                    //边界检测
                    actionFlag = 1;
                    if (getScrollY() > 0) {
                        scrollBy(0, (int) (-dy / damp));
                    } else {
                        close();
                    }
                } else {
                    //上滑
                    //边界检测
                    actionFlag = 2;
                    if (getScrollY() < layoutHeight) {
                        scrollBy(0, (int) (-dy / damp));
                    } else {
                        open();
                    }
                }

                lastPoint.set(currPoint.x, currPoint.y);
                break;

            case MotionEvent.ACTION_UP:
                lastPoint.set((int) event.getX(), (int) event.getY());
                double judge = (double) getScrollY() / (double) layoutHeight;
                switch (actionFlag) {
                    case 0:
                        break;
                    case 1:
                        if (1 - judge > this.judge) {
                            close();
                            break;
                        } else {
                            open();
                        }
                        break;

                    case 2:
                        if (judge > this.judge) {
                            open();
                            break;
                        } else {
                            close();
                        }
                        break;
                }
                actionFlag = 0;
                break;
        }
        return true;
    }


    public void toggle() {
        if (statue == statue_close) {
            open();
        } else if (statue == statue_open) {
            close();
        }
    }

    public void open() {
        int cy = getScrollY();
        mScroller.startScroll(0, cy, 0, layoutHeight - cy, 1000);
        invalidate();
        statue = statue_open;
    }

    public void close() {
        statue = statue_close;
        int cy = getScrollY();
        mScroller.startScroll(0, cy, 0, -cy, 1000);
        invalidate();
    }


    @Override
    public void computeScroll() {


        transformTopView();


        if (mScroller.computeScrollOffset()) {
            scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
            invalidate();
        }
    }

    private void transformTopView() {
        mTopView.setTranslationY(getScaleY());
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        if (getChildCount() != 2) {
            throw new IllegalStateException("TBMerchandiseLayout have to add two layout!!!");
        }

        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        layoutWidth = widthSize;
        layoutHeight = heightSize;

        System.out.println("width = " + widthSize);
        System.out.println("height = " + heightSize);

        mTopView = getChildAt(0);
        mBottomView = getChildAt(1);

        //measure child View
        mTopView.measure(widthMeasureSpec, heightMeasureSpec);
        mBottomView.measure(widthMeasureSpec, heightMeasureSpec);

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}
